//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region Namespaces
    using System;
    using System.Collections.Generic;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using System.Collections.ObjectModel;
    #endregion

    /// <summary>
    /// Class containing information to create a well formed activity tree.
    /// </summary>
    internal class WellFormedConditionalTreeInfo
    {
        public WellFormedConditionalTreeInfo(string ownerName, SimpleActivityModel model, int level)
        {
            if (string.IsNullOrEmpty(ownerName))
            {
                throw new ArgumentNullException("ownerName");
            }
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            this.currentModel = model;
            this.level = level;
        }

        private string ownerIfElseCondition = string.Empty;

        private SimpleActivityModel currentModel;

        private Collection<WellFormedConditionalTreeInfo> children;

        private WellFormedConditionalTreeInfo parent;

        private int level;


        public SimpleActivityModel CurrentModel
        {
            get { return currentModel; }
        }

        public Collection<WellFormedConditionalTreeInfo> Children
        {
            get { return children; }
        }


        public void AddChild(WellFormedConditionalTreeInfo model)
        {
            if (this.children == null)
            {
                this.children = new Collection<WellFormedConditionalTreeInfo>();
            }

            this.children.Add(model);
            model.parent = this;
        }

        public int Level
        {
            get { return level; }
        }

        public static bool ActivityModelExistsInTree(WellFormedConditionalTreeInfo treeInfo, SimpleActivityModel model)
        {
            bool result = false;
            if (treeInfo.currentModel == model)
            {
                return true;
            }
            if (treeInfo.children != null && treeInfo.children.Count > 0)
            {
                foreach (WellFormedConditionalTreeInfo childTreeInfo in treeInfo.children)
                {
                    result = ActivityModelExistsInTree(childTreeInfo, model);
                    if (result)
                    {
                        break;
                    }
                }
            }
            return result;
        }
    }
}
